package com.hunk.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Description :
 * 	Jdbc Utils
 * Files:
 * 	jdbc.properties
 *  JDBCUtils.java
 * @author HunkDeng
 * @since 2013-1-6
 */
public final class SqlHelper {

	private SqlHelper() {
    }

    private final static String url;
    private final static String user;
    private final static String password;
    private final static String driver;

    static {
		Properties prop = new Properties();
    	InputStream in = SqlHelper.class.getResourceAsStream("jdbc.properties");		
		try {
			prop.load(in);			
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		url = prop.getProperty("url");
		user = prop.getProperty("user");
		password = prop.getProperty("password");
		driver = prop.getProperty("driver");
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }

    /** free connection and so on
     * @author HunkDeng
     * @param rs
     * @param st
     * @param conn
     */
    public static void free(ResultSet rs, Statement st, Connection conn) {
        try {
            if (rs != null)
                rs.close();
        } catch (SQLException e) {
        } finally {
            try {
                if (st != null)
                    st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (conn != null)
                        conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    public static void executeBatchInsertOrUpdate(List<String> sqlList) {
		Connection con = null;
		Statement stmt = null;
		try {
			con = SqlHelper.getConnection();
			con.setAutoCommit(false);
			stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			for(String sql : sqlList){
				stmt.execute(sql);
			}
			con.commit();
			con.setAutoCommit(true);
		} catch (Exception e) {
			if(stmt != null){
				try {
					stmt.cancel();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			if(con != null){
				try {
					con.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			e.printStackTrace();
		}finally{
			SqlHelper.free(null, stmt, con);
		}
	}
    
    public static <T> List<T> query(String sql, SqlHelperCallBack<T> callback) {
    	List<T> result = new ArrayList<T>();
    	Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = SqlHelper.getConnection();
			stmt = con.prepareStatement(sql);
			rs = stmt.executeQuery();
			
			while (rs.next()) {
				result.add(callback.handlerResult(rs));
			}
		} catch (Exception e) {
			if(stmt != null){
				try {
					stmt.cancel();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			if(con != null){
				try {
					con.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			e.printStackTrace();
		}finally{
			SqlHelper.free(rs, stmt, con);
		}
		return result;
    }
    
    public interface SqlHelperCallBack<T> {
    	public abstract T handlerResult(ResultSet rs) throws SQLException;
    }
}

